package com.team2.cyber_eye;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

/**
 * TODO: document your custom view class.
 */
public class TitleBarView extends RelativeLayout
{
	public TitleBarView(Context context)
	{
		super(context);
		init(null, 0);
	}

	public TitleBarView(Context context, AttributeSet attrs)
	{
		super(context, attrs);
		init(attrs, 0);
	}

	public TitleBarView(Context context, AttributeSet attrs, int defStyle)
	{
		super(context, attrs, defStyle);
		init(attrs, defStyle);
	}

	public Activity parent;
	private void init(AttributeSet attrs, int defStyle)
	{
		if (!this.isInEditMode())
		{
			parent = (Activity)this.getContext();//fixme can this be another view and not an activity?
			this.setBackgroundResource(R.color.black);
			
			Typeface font1 = Typeface.createFromAsset(parent.getAssets(), "CanarsieSlab.ttf");
			
			int TitleSize = (int)getResources().getDimension(R.dimen.topbar_size);
	
			TextView label_title = new TextView(this.getContext());
			ImageView image_logo = new ImageView(this.getContext());
			ImageView image_settings = new ImageView(this.getContext());
	
			String activityName = parent.getClass().getSimpleName();
			
			if(!activityName.equals("Options") &&
					!activityName.equals("MapDirectionList") &&
					!activityName.equals("Home")){
				image_settings.setImageResource(R.drawable.ic_action_settings);
				this.addView(image_settings);
				
				image_settings.setOnClickListener(new OnClickListener(){
					@Override
					public void onClick(View v) {
						Intent intent = new Intent(parent, Options.class);
						parent.startActivity(intent);
						//parent.finish();
					}
				});
				
				RelativeLayout.LayoutParams layout1 = (RelativeLayout.LayoutParams)image_settings.getLayoutParams();
				layout1.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
				layout1.width = TitleSize;
				layout1.height = TitleSize;
				image_settings.setLayoutParams(layout1);
			}
						
			label_title.setId("Title".hashCode());
			label_title.setTypeface(font1);
			label_title.setTextSize(getResources().getDimension(R.dimen.topbar_text_size_medium));
			label_title.setTextColor(getResources().getColor(R.color.white));
			
			//label_title.setTextAppearance(this.getContext(), R.style.boldText);
			label_title.setBackgroundResource(R.color.black);
			label_title.setPadding(0, 10, 5, 0);
			this.addView(label_title);
			
			if(activityName.equals("Home")){
				image_logo.setImageResource(R.drawable.cyber_eye_logo_new);
			} else{
				image_logo.setImageResource(R.drawable.home);
			}
			
			this.addView(image_logo);
			
			RelativeLayout.LayoutParams layout2 = (RelativeLayout.LayoutParams)label_title.getLayoutParams();
			layout2.addRule(RelativeLayout.CENTER_IN_PARENT);
			layout2.leftMargin = TitleSize + (int)getResources().getDimension(R.dimen.medium_gap);
			label_title.setLayoutParams(layout2);
			
			RelativeLayout.LayoutParams layout3 = (RelativeLayout.LayoutParams)image_logo.getLayoutParams();
			layout3.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
			layout3.width = TitleSize;
			layout3.height = TitleSize;
			image_logo.setLayoutParams(layout3);		
			
			image_logo.setOnClickListener(new OnClickListener(){
				@Override
				public void onClick(View v) {
					if (parent.getClass() != Home.class)
					{
			    		Intent intent = new Intent(parent, Home.class);
			    		intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
			    		parent.startActivity(intent);
					}
				}
			});
		}
	}
	
	public void SetTitle(String Title)
	{
		TextView label_title = (TextView)this.findViewById("Title".hashCode());
		label_title.setText(Title);
	}
	
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

		if (this.isInEditMode())
		{
	        Paint p = new Paint();
	        p.setColor(getResources().getColor(R.color.green));
	        p.setStrokeWidth(5);
	        canvas.drawLine(0, 0, this.getWidth(), this.getHeight(), p);
	        canvas.drawLine(this.getWidth(), 0, 0, this.getHeight(), p);
        }
        
		ViewGroup.LayoutParams lp = this.getLayoutParams();
        lp.height = (int)getResources().getDimension(R.dimen.topbar_size);
        lp.width =  RelativeLayout.LayoutParams.MATCH_PARENT;
        this.setLayoutParams(lp);
    }
}
